//
//  MyBassketballPointTool.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/22.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MyBassketballPointTool.h"
#import "FirstFootballModel.h"

@interface MyBassketballPointTool ()

@property(nonatomic,strong)NSArray *checkAgainArray;

@property(nonatomic)BOOL isAgain;

@end

@implementation MyBassketballPointTool

singleton_implementation(MyBassketballPointTool);

static  int  indexOfSong;


static FMDatabase *_db;



-(void)creatTable
{
    //1.打开数据库
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"myPoint.sqlite"];
    
    NSLog(@"----path历史记录-----%@-------",path);
    
    _db = [FMDatabase databaseWithPath:path];
    [_db open];
    
    
    //2.创表
    BOOL  flag =   [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS Bassketball (id integer PRIMARY KEY, matchOrder text , leagueName text , matchTime text,  hostName text,visitName text , matchId text,visitGoal text,hostGoal text, statusDesc text,hostTeamId text,visitTeamId text,hostLogoUrl text,visitLogoUrl text,   isSelectPoint text);"];
    
    
    NSLog(@"创表---%d",flag);
    
}




-(void)addDataWithModel:(FirstFootballModel*)model
{
    //首先取出现有的歌曲
    
    self.checkAgainArray = [NSMutableArray array];
    self. checkAgainArray  = [self getModels];

    if(self.checkAgainArray.count != 0)
    {
        self.isAgain=NO;
        indexOfSong = 0;
        for (int i  =0; i < self.checkAgainArray.count; i ++)
        {
            if ([model.matchId  isEqualToString:[self.checkAgainArray[i]matchId]  ])
            {
                self.isAgain =YES;
                indexOfSong = i;
                break;
            }
            
        }
        
        
        if(self.isAgain ==YES)//思路如下:如果有相等的,先把原来的给删除,然后再添加进去
        {
            FirstFootballModel*musicModel = self.checkAgainArray[indexOfSong];
            
            [self deleteModel:musicModel];
            
        
            
            
           [_db  executeUpdate:@"INSERT INTO Bassketball(matchOrder, leagueName,matchTime,hostName,visitName,matchId,visitGoal,hostGoal,statusDesc,hostTeamId ,visitTeamId ,hostLogoUrl,visitLogoUrl,isSelectPoint) VALUES (?, ?,?, ?,?,?,?,?,?,?,?,?,?,?);", model.matchOrder, model.leagueName,model.matchTime,model.hostName,model.visitName,model.matchId,model.visitGoal,model.hostGoal,model.statusDesc,model.hostTeamId,model.visitTeamId, model.hostLogoUrl,model.visitLogoUrl,     [NSString stringWithFormat:@"%d",model.isSelectPoint ] ];
            
            
        }
        else
        {
            
            
           [_db  executeUpdate:@"INSERT INTO Bassketball(matchOrder, leagueName,matchTime,hostName,visitName,matchId,visitGoal,hostGoal,statusDesc,hostTeamId ,visitTeamId ,hostLogoUrl,visitLogoUrl,isSelectPoint) VALUES (?, ?,?, ?,?,?,?,?,?,?,?,?,?,?);", model.matchOrder, model.leagueName,model.matchTime,model.hostName,model.visitName,model.matchId,model.visitGoal,model.hostGoal,model.statusDesc,model.hostTeamId,model.visitTeamId, model.hostLogoUrl,model.visitLogoUrl,     [NSString stringWithFormat:@"%d",model.isSelectPoint ] ];
        }
        
        
    }
    
    else
    {
        
        
        
        
       [_db  executeUpdate:@"INSERT INTO Bassketball(matchOrder, leagueName,matchTime,hostName,visitName,matchId,visitGoal,hostGoal,statusDesc,hostTeamId ,visitTeamId ,hostLogoUrl,visitLogoUrl,isSelectPoint) VALUES (?, ?,?, ?,?,?,?,?,?,?,?,?,?,?);", model.matchOrder, model.leagueName,model.matchTime,model.hostName,model.visitName,model.matchId,model.visitGoal,model.hostGoal,model.statusDesc,model.hostTeamId,model.visitTeamId, model.hostLogoUrl,model.visitLogoUrl,   [NSString stringWithFormat:@"%d",model.isSelectPoint ] ];
    }
    
}

//取出
-(NSArray *)getModels
{
    //得到结果集
    FMResultSet *set = [_db  executeQuery:@"SELECT * FROM Bassketball;"];
    
    
    //不段的往下取数据
    NSMutableArray *modesArray = [NSMutableArray array];
    
    while (set.next) {
        
        
        
        FirstFootballModel *model = [[FirstFootballModel alloc]init];
        model.matchOrder = [set  stringForColumn:@"matchOrder"];
        model.leagueName = [set stringForColumn:@"leagueName"];
        model.matchTime = [set stringForColumn:@"matchTime"];
        model.hostName = [set stringForColumn:@"hostName"];
        
        model.visitName = [set stringForColumn:@"visitName"];
        model.matchId = [set stringForColumn:@"matchId"];
        
        model.visitGoal =[set stringForColumn:@"visitGoal"];
        model.hostGoal =[set stringForColumn:@"hostGoal"];
        model.statusDesc =[set stringForColumn:@"statusDesc"];
        
        
        model.hostTeamId =[set stringForColumn:@"hostTeamId"];
        model.visitTeamId =[set stringForColumn:@"visitTeamId"];
        
        
        model.hostLogoUrl = [set stringForColumn:@"hostLogoUrl"];
        model.visitLogoUrl = [set stringForColumn:@"visitLogoUrl"];
        
        
        model.isSelectPoint = [[set stringForColumn: @"isSelectPoint"]boolValue];
        
        
        [modesArray addObject:model];
        
    }
    
    return modesArray;
    
}


//删除

-(void)deleteModel:(FirstFootballModel*)model
{
    
   [_db  executeUpdate:@"delete from Bassketball  where  matchId  = ?", model.matchId];
    
    
}



@end
